package frontend.container.taskPanel;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;

import frontend.base.BasicPanel;

public class JTaskXpanel extends BasicPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1115832909233773879L;

	/**
	 * the panel to store the title
	 */
	private BasicPanel titlePanel = new BasicPanel();

	private JLabel titleLbl= new JLabel();
	
	
	/**
	 * the panel to load task panel
	 */
	private BasicPanel taskListPanel = new BasicPanel();
	GridBagConstraints gbc = new GridBagConstraints();
	int index = 0;
	/**
	 * the status to indicate the collapse.
	 */
	private boolean isCollapsed = false;

	/**
	 * this task panel's task list
	 */
	private List<TaskLabel> taskList = new ArrayList<TaskLabel>();

	public JTaskXpanel() {
		initTitlePanel();
		initTaskListPanel();
		initLayout();
		initListener();
	}

	private void initTitlePanel() {
		titlePanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		titlePanel.add(titleLbl, gbc);
	}
	
	private void initListener() {
		
		titlePanel.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				setCursor(new Cursor(Cursor.HAND_CURSOR));
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				
				if (isCollapsed) {
					taskListPanel.setVisible(true);
				} else {
					taskListPanel.setVisible(false);
				}
				isCollapsed = !isCollapsed;
			}
		});
	}

	private void initLayout() {

		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		int indexY = 0;
		gbc.gridx = 0;
		gbc.gridy = indexY++;
		gbc.weightx = 1;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.insets = new Insets(5, 5, 5, 5);
		add(titlePanel, gbc);
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.gridy = indexY++;
		add(taskListPanel, gbc);
	}

	private void initTaskListPanel() {
		taskListPanel.setLayout(new GridBagLayout());
	}
	
	public boolean isCollapsed() {
		return isCollapsed;
	}

	public void setCollapsed(boolean isCollapsed) {
		this.isCollapsed = isCollapsed;
	}

	public List<TaskLabel> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<TaskLabel> taskList) {
		this.taskList = taskList;
	}

	public void setTask(TaskLabel taskLabel) {
		this.taskList.add(taskLabel);
		gbc.gridx = 0;
		gbc.weightx = 1;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridy = index++;
		gbc.insets = new Insets(5, 15, 5, 5);
		taskListPanel.add(taskLabel, gbc);
	}


	public void setTitleName(String title) {
		titleLbl.setText(title);
	}

}
